#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h> 

#define PORT_NUM 2323
#define ADDRESS "127.0.0.1"

int main(int argc, char *argv[])
{
    int reuse = 1;
    struct sockaddr_in serv_addr;
    int sockfd, i;
    socklen_t slen = sizeof(serv_addr);

	char buffer[4096];

	if (argc != 2) {
		fprintf(stderr,"usage %s [data_to_be_sent]\n", argv[0]);
		exit(1);
	}

    bzero(buffer,sizeof(buffer));
    strcat(buffer, argv[1]);

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd < 0){
		perror("socket");
		exit(1);
    }

    bzero(&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(2323);
    if (inet_aton(ADDRESS, &serv_addr.sin_addr) == 0) {
        fprintf(stderr, "inet_aton() failed\n");
        exit(1);
    }

    if (sendto(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&serv_addr, slen) == -1) {
        perror("sendto");
    }

    bzero(buffer, sizeof(buffer));

    if (recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&serv_addr, &slen) == -1) {
        perror("recvfrom");
    }

    printf("%s\n", buffer);

	return 0;
}
